import type { Equal, Expect } from "@type-challenges/utils";

type X = Promise<string>;
type Y = Promise<{ field: number }>;
type Z = Promise<Promise<string | number>>;
type Z1 = Promise<Promise<Promise<string | boolean>>>;

type cases = [
  Expect<Equal<MyAwaited<X>, string>>,
  Expect<Equal<MyAwaited<Y>, { field: number }>>,
  Expect<Equal<MyAwaited<Z>, string | number>>,
  Expect<Equal<MyAwaited<Z1>, string | boolean>>
];

// @ts-expect-error
type error = MyAwaited<number>;

// type MyAwaited<T extends Promise<infer Y>>  这样就会报错
// 递归调用
type MyAwaited<T extends Promise<any>> = T extends Promise<infer X>
  ? X extends Promise<infer Y>
    ? MyAwaited<X>
    : X
  : never;

// infer
// 1. 只能在条件类型里面使用
// 2. 设置位置  变量
